package tmt.usercenter.web.domain.security.oauth2;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.provider.ClientDetails;
import tmt.usercenter.web.domain.ClientSystem;
import tmt.usercenter.web.domain.security.TmtGrantedAuthority;

import java.util.*;
import java.util.stream.Collectors;

public class ClientDetailsImpl implements ClientDetails {

    private String clientId;

    private String clientSecret;

    private Set<String> resourceIds;

    private boolean secretRequired;

    private boolean scoped;

    private Set<String> scope;

    private Set<String> authorizedGrantTypes;

    private Set<String> registeredRedirectUri;

    private List<GrantedAuthority> grantedAuthorities = new ArrayList<>();

    private int accessTokenValiditySeconds;

    private int refreshTokenValiditySeconds;

    private boolean autoApprove;


    public ClientDetailsImpl(ClientSystem clientSystem){
        this.clientId = clientSystem.getClientId();
        this.clientSecret = clientSystem.getClientSecret();
        this.secretRequired = clientSystem.isSecretRequired();
        this.scoped = clientSystem.isScoped();
        this.accessTokenValiditySeconds = clientSystem.getAccessTokenValiditySeconds();
        this.refreshTokenValiditySeconds = clientSystem.getRefreshTokenValiditySeconds();
        this.autoApprove = clientSystem.isAutoApprove();

        this.resourceIds = new HashSet<String>(){{
            addAll(clientSystem.getResourceIds());
        }};
        this.scope =  new HashSet<String>(){{
            addAll(clientSystem.getScope());
        }};
        this.authorizedGrantTypes =  new HashSet<String>(){{
            addAll(clientSystem.getAuthorizedGrantTypes().stream().map(Enum::name).collect(Collectors.toList()));
        }};
        this.registeredRedirectUri =  new HashSet<String>(){{
            addAll(clientSystem.getRegisteredRedirectUri());
        }};

        clientSystem.getTmtAuthorities().forEach(x-> grantedAuthorities.add(new TmtGrantedAuthority(x.getAuthority())));
    }


    @Override
    public String getClientId() {
        return clientId;
    }

    @Override
    public Set<String> getResourceIds() {
        return resourceIds;
    }

    @Override
    public boolean isSecretRequired() {
        return secretRequired;
    }

    @Override
    public String getClientSecret() {
        return clientSecret;
    }

    @Override
    public boolean isScoped() {
        return scoped;
    }

    @Override
    public Set<String> getScope() {
        return scope;
    }

    @Override
    public Set<String> getAuthorizedGrantTypes() {
        return authorizedGrantTypes;
    }

    @Override
    public Set<String> getRegisteredRedirectUri() {
        return registeredRedirectUri;
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        return grantedAuthorities;
    }

    @Override
    public Integer getAccessTokenValiditySeconds() {
        return accessTokenValiditySeconds;
    }

    @Override
    public Integer getRefreshTokenValiditySeconds() {
        return refreshTokenValiditySeconds;
    }

    @Override
    public boolean isAutoApprove(String scope) {
        return autoApprove;
    }

    @Override
    public Map<String, Object> getAdditionalInformation() {
        return null;
    }
}
